package scr 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import img.ScalePlaneNoText;
	import ru.gosoplata.model.TmpModel;
	import ru.gosoplata.view.FooterView;
	import uicontrols.ButtonBase;
	/*
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenConfirmUserData extends ScreenBase implements IScreen
	{
		private var inputtxt:TextField;
		private var pic_count:Number=0;
		private	var btn_next:ButtonBase = new ButtonBase("next", 0x00FF00);

		private var pics_info:Array = new Array(new Array("plane_big", TmpModel.IMG_FOLDER+"/podtvezdenie_nomera/images/plane_big_platezi.png"),
												new Array("plane_tel", TmpModel.IMG_FOLDER+"/podtvezdenie_nomera/images/plane_tel.png"),
												new Array("text_info", TmpModel.IMG_FOLDER+"/podtvezdenie_nomera/images/text_dannii.png"),
												new Array("plane_logo", TmpModel.IMG_FOLDER + "/podtvezdenie_nomera/images/plane_logo.png"));
		
		private var curr_pic:Array;
		
		private var plane_tel_x:Number;
		private var plane_tel_y:Number;
		private var plane_tel_wd:Number;
		private var text_y:Number;
		private var logo:Bitmap;
		private var scale_plane:ScalePlaneNoText;
		
		private var _footer:FooterView = new FooterView();
		
		public function ScreenConfirmUserData() 
		{
			super("confirm_userdata");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			addChild(_footer);
			
			btn_next.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_next.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_next);
			pic_count++
			pics_info = pics_info.reverse();
			loadPictures();
		}
		
		override public function reDraw():void
		{
			_footer.reDraw();
			
			inputtxt.text = TmpModel.payment.accauntnumber;
			loadlogo();
		}
		
		private function loadlogo():void 
		{
			var req:URLRequest = new URLRequest(ru.gosoplata.model.Provider.getImagePath(TmpModel.payment.provider.providerXML));
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLogoLoaded);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLogoLoadError);
			loader.load(req);
		}
		
		private function onLogoLoadError(e:IOErrorEvent):void 
		{
			trace("onLogoLoadError " + e);
		}
		
		private function onLogoLoaded(e:Event):void 
		{
			(getChildByName("prov_logo") as Bitmap).bitmapData = Bitmap(Loader(e.target.loader).content).bitmapData;
		}
		
		private function loadPictures():void {
			if(pics_info.length > 0){
				curr_pic = (pics_info.pop() as Array);
				
				var url:String = TmpModel.IMG_FOLDER+"/first_page/images/flag.png";
				url = curr_pic[1];
				var req:URLRequest = new URLRequest(url);
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { } );
				loader.load(req);
			}
		}
		
		private function onLoadComplete(e:Event):void 
		{
			var ldr:Loader = Loader(e.target.loader);
			var image:Bitmap = Bitmap(ldr.content);
			placeImage(image);
		}
		
		public function placeImage(image:Bitmap):void {
			switch(curr_pic[0]) {
				case "plane_big":
					image.x = stage.stageWidth/2-image.width/2;
					image.y = stage.stageHeight/2 - image.height/2;
					addChild(image);
					break;
				case "plane_tel":
					image.x = 300;
					image.y = stage.stageHeight / 2 - image.height / 2 + 50;
					plane_tel_x = image.x;
					plane_tel_y = image.y;
					plane_tel_wd = image.width;
					scale_plane = new ScalePlaneNoText(700);
					scale_plane.x = plane_tel_x;
					scale_plane.y = plane_tel_y;
					addChild(scale_plane);
					inputtxt = new TextField();
					var fmt:TextFormat = TmpModel.getTextFormat(44, 0xFFFFFF,true);
					fmt.letterSpacing = 5;
					inputtxt.embedFonts = true;
					inputtxt.x = plane_tel_x+15;
					inputtxt.y = plane_tel_y+7;
					inputtxt.width = 670;
					inputtxt.autoSize = TextFieldAutoSize.CENTER;
					inputtxt.defaultTextFormat = fmt;
					inputtxt.antiAliasType = AntiAliasType.ADVANCED;
					inputtxt.text = TmpModel.payment.accauntnumber;
					
					addChild(inputtxt);
					break;
				case "plane_logo":
					image.x = 200;
					image.y = text_y-20;
					addChild(image);
					logo = new Bitmap();
					logo.x = image.x;
					logo.y = image.y;
					logo.name = "prov_logo";
					addChild(logo);
					break;
				case "text_info":
					image.x = 420;
					image.y = 380;
					text_y = image.y;
					addChild(image);
					break;
				default:
					image.x = stage.stageWidth/2-image.width/2;
					image.y = stage.stageHeight - image.height;
					addChild(image);
				break;
			}
			loadPictures();
		}
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) 
			{
				btn_next.x = stage.stageWidth - (btn_next.width + 20)
				btn_next.y = stage.stageHeight - (btn_next.height + 20);
				
			}
		}
		
		public function addInputField():void 
		{
			inputtxt = new TextField();
			inputtxt.width = 300;
			inputtxt.height = 100;
			inputtxt.multiline = true;
			inputtxt.border = true;
			inputtxt.selectable = false;
			addChild(inputtxt);
		}

		private function pressBtn(e:Event):void 
		{
			switch(e.target.name) 
			{
				case "next":
					TmpModel.tmpGlobalController.controllerValidatorOn();
					break;
			}
		}
		
	}

}